package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.SmsSendConsumeCodeRequest;
import com.frogs.ucenter.v1.request.SmsVerifyConsumeCodeRequest;
import com.frogs.ucenter.v1.request.UserBalanceConsumeRequest;
import com.frogs.ucenter.v1.request.UserRechargeBalanceBlockingRequest;
import com.frogs.ucenter.v1.request.UserRechargeBalanceReleaseRequest;
import com.frogs.ucenter.v1.request.UserRechargeBalanceUnfreezeRequest;
import com.frogs.ucenter.v1.request.UserRechargeConsumeRecordListRequest;
import com.frogs.ucenter.v1.service.UserRechargeConsumeRecordService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userConsumeController")
@RequestMapping("/" + ApiConstants.VERSION )
public class UserConsumeController extends BaseController3 {

	private static final String ROOT = "/user";
	private static final String USER_CONSUME_ADD = ROOT + "/consume/add";
	private static final String USER_CONSUME_CODE_SEND = ROOT + "/consume/code/send";
	private static final String USER_CONSUME_CODE_VERIFY = ROOT + "/consume/code/verify";
	private static final String USER_CONSUME_RECORD_LIST = ROOT + "/consume/list";
	private static final String USER_CONSUME_BLOCKING = ROOT + "/consume/blocking";
	private static final String USER_CONSUME_UNFREEZE = ROOT + "/consume/unfreeze";
	private static final String USER_CONSUME_RELEASE = ROOT + "/consume/release";
	
	@Resource(name=ApiConstants.VERSION+"userRechargeConsumeRecordService")
	private UserRechargeConsumeRecordService userRechargeConsumeRecordService;
	

	/**
	 * 用户消费接口
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_CONSUME_ADD)
    public Response userBalanceConsumeAdd(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserBalanceConsumeRequest balanceConsumeRequest = new UserBalanceConsumeRequest();
        	balanceConsumeRequest.parse(request);
        	response = userRechargeConsumeRecordService.balanceConsume(balanceConsumeRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
	
	/**
	 * 发送消费验证码
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_CONSUME_CODE_SEND)
    public Response userConsumeCodeSend(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	SmsSendConsumeCodeRequest sendConsumeCodeRequest = new SmsSendConsumeCodeRequest();
        	sendConsumeCodeRequest.parse(request);
        	response = userRechargeConsumeRecordService.sendConsumeCode(sendConsumeCodeRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
        return response;
    }
	
	/**
	 * 验证消费验证码
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(USER_CONSUME_CODE_VERIFY)
	public Response userConsumeCodeVerify(HttpServletRequest request, ModelMap model) {
		Response response = null ; 
		try{
			SmsVerifyConsumeCodeRequest verifyConsumeCodeRequest = new SmsVerifyConsumeCodeRequest();
			verifyConsumeCodeRequest.parse(request);
			response = userRechargeConsumeRecordService.verifyConsumeCode(verifyConsumeCodeRequest);
		}catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	

	/**
	 * 消费记录列表
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_CONSUME_RECORD_LIST)
	public Response userConsumeRecordList(HttpServletRequest request) {
		Response response;
		try {
			UserRechargeConsumeRecordListRequest listRequest = new UserRechargeConsumeRecordListRequest();
			listRequest.parse(request);
			response = userRechargeConsumeRecordService.listConsume(listRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}

	/**
	 * 冻结余额
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_CONSUME_BLOCKING)
	public Response userConsumeBlocking(HttpServletRequest request) {
		Response response;
		try {
			UserRechargeBalanceBlockingRequest blockingRequest = new UserRechargeBalanceBlockingRequest();
			blockingRequest.parse(request);
			response = userRechargeConsumeRecordService.blockingRechargeBalance(blockingRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 解冻余额
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_CONSUME_UNFREEZE)
	public Response userConsumeUnfreeze(HttpServletRequest request) {
		Response response;
		try {
			UserRechargeBalanceUnfreezeRequest unfreezeRequest = new UserRechargeBalanceUnfreezeRequest();
			unfreezeRequest.parse(request);
			response = userRechargeConsumeRecordService.unfreezeRechargeBalance(unfreezeRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 消除冻结余额
	 * @param request
	 * @return
	 */
	@RequestMapping(USER_CONSUME_RELEASE)
	public Response userConsumeRelease(HttpServletRequest request) {
		Response response;
		try {
			UserRechargeBalanceReleaseRequest releaseRequest = new UserRechargeBalanceReleaseRequest();
			releaseRequest.parse(request);
			response = userRechargeConsumeRecordService.releaseBlockingBalance(releaseRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
}
